package rp.lee.base;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PlayEventDispatchImpl implements PlayInterface {

	public static List<PlayInterface> list = new ArrayList<PlayInterface>();
	static boolean isOperating = false;
	public static synchronized void add(PlayInterface pi) {
		isOperating = true;
		list.add(pi);
		isOperating = false;
	}
	
	public static synchronized void delete(PlayInterface pi) {
		
		while (isOperating) {
		}
		Iterator<PlayInterface>  iterator = list.iterator();
		PlayInterface mpi = null;
		while (iterator.hasNext()) {
			mpi = iterator.next();
			if(pi == mpi) {
				iterator.remove();
			}
		}
	}
	@Override
	public synchronized void next(PlayWarper mPlayWarper) {
		while (isOperating) {
		}
		for (PlayInterface mPlayInterface : list) {
			mPlayInterface.next(mPlayWarper);
		}
	}

	@Override
	public synchronized void pause() {
		while (isOperating) {
		}
		for (PlayInterface mPlayInterface : list) {
			mPlayInterface.pause();
		}
	}

	@Override
	public synchronized void pre(PlayWarper mPlayWarper) {
		while (isOperating) {
		}
		for (PlayInterface mPlayInterface : list) {
			mPlayInterface.pre(mPlayWarper);
		}
	}

	@Override
	public synchronized void start(PlayWarper mPlayWarper, int milliseconds) {
		while (isOperating) {
		}
		try {
			for (PlayInterface mPlayInterface : list) {
				mPlayInterface.start(mPlayWarper, milliseconds);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public synchronized void stop() {
		
		while (isOperating) {
		}
		try {
			for (PlayInterface mPlayInterface : list) {
				mPlayInterface.stop();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void updateUI(String currPlayTime, int playTime) {
		while (isOperating) {
		}
		for (PlayInterface mPlayInterface : list) {
			mPlayInterface.updateUI(currPlayTime, playTime);
		}
	}
}